import React from "react";
import {useSearchParams} from "react-router-dom";
import {useRequest} from "ahooks";
import {getQuestionListService} from "../services/question";
import {LIST_PAGE_KEY, LIST_PAGESIZE_KEY, LIST_SEARCH_KEY} from "../constant";

//实现一个功能，完成对于搜索框的请求
//对于搜索框来说，不应该得到的是一个数据，应该是一个list数据
type OptionType = {
    isStar:boolean,
    isDeleted:boolean,
    page:number,
    pageSize:number
}
function useQuestionSearch(opt:Partial<OptionType>={}){
     const {isStar ,isDeleted}  = opt;
    //获取搜索框的动态变化
    const [searchPathms] = useSearchParams();
    const {data,loading,error,refresh} =  useRequest( async  ()=> {
          const keyword  = searchPathms.get(LIST_SEARCH_KEY) || "";
          //从url中解析出page和pageSize;
          const page =  parseInt(searchPathms.get(LIST_PAGE_KEY)||'') || 1;
          const pageSize =  parseInt(searchPathms.get(LIST_PAGESIZE_KEY)||'') || 10;
          // console.log(pageSize+"???"+page+"????");
          // await getQuestionListService({keyword});
          return  await getQuestionListService({keyword,isStar,isDeleted,page,pageSize});
    },{
        refreshDeps:[searchPathms]  //刷新依赖项
    })

    return {data,error,loading,refresh};
}
export default useQuestionSearch;